package com.ruitu.health_1.ui;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.support.widget.dialog.CustomAlertDialog;
import com.ruitu.health_1.R;
import com.ruitu.health_1.dao.bean.GoodsInfo;
import com.ruitu.health_1.dao.bean.Order;
import com.ruitu.health_1.dao.bean.OrderDetail;
import com.ruitu.health_1.dao.bean.OrderItemList;
import com.ruitu.health_1.dao.bean.Pay;
import com.ruitu.health_1.mvp.model.OrderModel;
import com.ruitu.health_1.mvp.presenter.OrderPresenter;
import com.ruitu.health_1.util.CustomDialogUtil;
import com.ruitu.health_1.util.CustomInterface;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.bean.user.Address;
import com.ruitu.router_module.util.ReqUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderDetailsActivity extends BaseActivity<OrderPresenter.OrderDetailsP, OrderModel.OrderM> {

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_order_details;
    }

    private RecyclerView recyclerview;
    private ChildAdapter adapter;
    private TextView tv_consignee;
    private TextView tv_tel;
    private TextView tv_address;
    private TextView tv_waiting_pay_actual_money;//支付金额
    private TextView tv_waiting_pay_commodity_total;//商品总价
    private TextView tv_waiting_pay_order_total;//订单总价
    private TextView tv_waiting_pay_expressage_total;//运费价格
    private TextView tv_order_number;//订单编号
    private TextView tv_create_time;//订单创建时间
    private TextView tv_pay_time;//支付时间
    private TextView tv_pay_type;//支付方式
    private TextView tv_order_status;//订单的状态
    private TextView tv_go_pay;//去支付
    private TextView tv_cancel;//取消
    private TextView tv_check_logistics;//查看物流
    private TextView tv_confirm_receipt;//确认收货
    private TextView tv_comment;//评论
    private TextView tv_buy_again;//再次购买

    private LinearLayout ll_wait_pay;
    private LinearLayout ll_wait_send;
    private LinearLayout ll_shipped;
    private LinearLayout ll_complete;
    private Bundle bundle;
    private String orderId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHeadTitle("订单详情");
        bundle = getIntent().getExtras();
        orderId = (String) bundle.get("orderId");

        showProgress();
        String id = AppHolder.instance().getUser().getId();
        p.getOrderItem(id, orderId);

    }

    private CustomAlertDialog dialog;
    private CustomDialogUtil dialogUtil;

    private void setOnClick() {
        tv_go_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Pay pay = new Pay();
                pay.setOrderId(info.getId());
                pay.setPayAmount(info.getPayAmount());
                Address address = new Address();
                address.setName(info.getReceiverName());
                address.setPhoneNumber(info.getReceiverPhone());
                address.setProvince(info.getReceiverProvince());
                address.setCity(info.getReceiverCity());
                address.setRegion(info.getReceiverRegion());
                address.setDetailAddress(info.getReceiverDetailAddress());
                Bundle bundle = new Bundle();
                bundle.putSerializable("address", address);
                bundle.putSerializable("pay", pay);
                startActivity(PayOrderActivity.class, bundle);
            }
        });

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = new CustomAlertDialog(OrderDetailsActivity.this, "确定取消当前订单么?", new CustomAlertDialog.OkOnClickListener() {
                    @Override
                    public void OkOnClick() {
                        Map<String, String> map = new HashMap<>();
                        map.put("orderId", info.getId());
                        map.put("memberId", AppHolder.instance().getUser().getId());
                        p.cancelOrder(ReqUtil.getBody(map, true));
                    }
                });
                dialog.show();
            }
        });

        tv_confirm_receipt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (orderItemList.size() <= 1) {
                    String orderIds = new Gson().toJson(ids());
                    final Map map = new HashMap<>();
                    map.put("ids", orderIds);
                    map.put("memberId", AppHolder.instance().getUser().getId());
                    //单个订单的处理
                    dialogUtil = new CustomDialogUtil(OrderDetailsActivity.this, "您是否收到该订单商品?", "已收货", "未收货", new CustomInterface.OnBtnClickListener() {
                        @Override
                        public void onLeftClick() {
                            showProgress();
                            p.confirmReceipt(ReqUtil.getBody(map, true));
                        }

                        @Override
                        public void onRightClick() {
                            dialogUtil.dismiss();
                        }
                    });
                    dialogUtil.show();
                    return;
                }
                String orderIds = new Gson().toJson(ids());
                final Map map = new HashMap<>();
                map.put("ids", orderIds);
                map.put("memberId", AppHolder.instance().getUser().getId());
                //单个订单的处理
                //选择确认收货
                dialogUtil = new CustomDialogUtil(OrderDetailsActivity.this, "请选择确认收货方式", "全部确认", "单选确认"
                        , new CustomInterface.OnBtnClickListener() {
                    @Override
                    public void onLeftClick() {

                        showProgress();
                        p.confirmReceipt(ReqUtil.getBody(map, true));
                    }

                    @Override
                    public void onRightClick() {
                        Bundle bundle = new Bundle();
                        bundle.putParcelableArrayList("order", orderItemList);
                        startActivity(ShippedActivity.class, bundle);
                    }
                });
                dialogUtil.show();


            }
        });

        tv_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (orderItemList.size() <= 1) {
                    Bundle bundle = new Bundle();
                    OrderItemList orderItemList1 = orderItemList.get(0);
                    bundle.putString("orderId", orderItemList1.getId());
                    bundle.putString("pic", orderItemList1.getProductPic());
                    startActivity(CommentActivity.class, bundle);
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putSerializable("order", orderItemList);
                startActivity(CommentListActivity.class, bundle);
            }
        });

        tv_buy_again.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

    }

    List idList;

    //获取所有确认收货订单的id
    private List<String> ids() {
        idList = new ArrayList<>();
        for (int i = 0; i < orderItemList.size(); i++) {
            idList.add(orderItemList.get(i).getId());
        }
        return idList;
    }

    @Override
    protected void initViews() {
        super.initViews();
        tv_consignee = findViewById(R.id.tv_consignee);
        tv_tel = findViewById(R.id.tv_tel);
        tv_address = findViewById(R.id.tv_address);
        tv_waiting_pay_actual_money = findViewById(R.id.tv_waiting_pay_actual_money);
        tv_waiting_pay_commodity_total = findViewById(R.id.tv_waiting_pay_commodity_total);
        tv_waiting_pay_order_total = findViewById(R.id.tv_waiting_pay_order_total);
        tv_waiting_pay_expressage_total = findViewById(R.id.tv_waiting_pay_expressage_total);
        tv_order_number = findViewById(R.id.tv_order_number);
        tv_create_time = findViewById(R.id.tv_creat_time);
        tv_pay_time = findViewById(R.id.tv_creat_time);
        tv_pay_type = findViewById(R.id.tv_pay_type);
        tv_order_status = findViewById(R.id.tv_order_status);
        tv_go_pay = findViewById(R.id.tv_go_pay);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_check_logistics = findViewById(R.id.tv_check_logistics);
        tv_confirm_receipt = findViewById(R.id.tv_confirm_receipt);
        tv_comment = findViewById(R.id.tv_comment);
        tv_buy_again = findViewById(R.id.tv_buy_again);

        ll_wait_pay = findViewById(R.id.ll_wait_pay);
        ll_wait_send = findViewById(R.id.ll_wait_send);
        ll_shipped = findViewById(R.id.ll_shipped);
        ll_complete = findViewById(R.id.ll_complete);
        recyclerview = findViewById(R.id.recyclerview);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerview.setLayoutManager(manager);
        adapter = new ChildAdapter(this);
        recyclerview.setAdapter(adapter);
        setOnClick();
    }

    private void initAddress() {
        tv_consignee.setText(info.getReceiverName());
        tv_tel.setText(info.getReceiverPhone());
        tv_address.setText(info.getReceiverProvince() + info.getReceiverCity() + info.getReceiverRegion() + info.getReceiverDetailAddress());
        initOrderInfo();
    }

    private void initOrderInfo() {
        tv_waiting_pay_commodity_total.setText(info.getTotalAmount() + "元");
        tv_waiting_pay_order_total.setText(info.getTotalAmount() + "元");
        tv_waiting_pay_actual_money.setText("￥" + info.getTotalAmount());
        tv_waiting_pay_expressage_total.setText(TextUtils.isEmpty(info.getFreightAmount()) ? "包邮" : info.getFreightAmount());
        tv_order_number.setText(info.getOrderSn());
        tv_create_time.setText(info.getCreateTime());
//        tv_pay_time.setText(TimeUtils.millis2String(orderDetail.getPayTime()));
        String status = info.getStatus();
        if (TextUtils.equals(status, "0")) {
            ll_wait_pay.setVisibility(View.VISIBLE);
            tv_order_status.setText("未支付");
        } else if (TextUtils.equals(status, "1")) {
            ll_wait_send.setVisibility(View.VISIBLE);
            tv_order_status.setText("待发货");
        } else if (TextUtils.equals(status, "2")) {
            ll_shipped.setVisibility(View.VISIBLE);
            tv_order_status.setText("已发货");
        } else if (TextUtils.equals(status, "3")) {
            ll_complete.setVisibility(View.VISIBLE);
            tv_order_status.setText("已收货");
        }

        if (!TextUtils.isEmpty(info.getPayType())) {
            switch (info.getPayType()) {
                case "0":
                    tv_pay_type.setText("微信");
                    break;
                case "1":
                    tv_pay_type.setText("支付宝");
                    break;
                case "3":
                    tv_pay_type.setText("余额支付");
                    break;
            }

        }

    }

    private Order info;
    private ArrayList<OrderItemList> orderItemList;

    public void setData(OrderDetail orderDetail) {
        this.info = orderDetail.getOrder();
        this.orderItemList = orderDetail.getOrderItemList();

        adapter.setDataList(orderDetail.getOrderItemList());
        initAddress();
    }

    class ChildAdapter extends ListBaseAdapter<OrderItemList> {

        public ChildAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_detail_product;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            ImageView iv_commodity = holder.getView(R.id.iv_commodity);
            TextView tv_commodity_name = holder.getView(R.id.tv_commodity_name);
            TextView tv_commodity_info = holder.getView(R.id.tv_commodity_info);
            TextView tv_commodity_price = holder.getView(R.id.tv_commodity_price);
            TextView tv_commodity_num = holder.getView(R.id.tv_commodity_num);
            OrderItemList orderItemList = mDataList.get(position);
            Glide.with(mContext).load(orderItemList.getProductPic()).apply(Arad.opts).into(iv_commodity);
            GoodsInfo goodsInfo = orderItemList.getGood();
            tv_commodity_name.setText(orderItemList.getProductName());
            StringBuffer buffer = new StringBuffer();
            if (goodsInfo != null) {
                if (!TextUtils.isEmpty(goodsInfo.getSp1())) {
                    buffer.append(goodsInfo.getSp1());
                }
                if (!TextUtils.isEmpty(goodsInfo.getSp2())) {
                    buffer.append("," + goodsInfo.getSp2());
                }
                if (!TextUtils.isEmpty(goodsInfo.getSp3())) {
                    buffer.append("," + goodsInfo.getSp3());
                }
            }
            tv_commodity_info.setText(buffer.toString());
            tv_commodity_num.setText("x" + goodsInfo.getProductQuantity());
            tv_commodity_price.setText(goodsInfo.getProductPrice());
        }
    }
}
